<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="楼栋" prop="floorId">
     <!-- <el-input v-model="dataForm.floorId" placeholder="楼栋"></el-input> -->
			  <el-select v-model="dataForm.floorId" placeholder="请选择" @change="getFloorChidList(1,dataForm.floorId)">
			      <el-option
			        v-for="item in floorDataList"
			        :key="item.id"
			        :label="item.name"
			        :value="item.id">
			      </el-option>
			  </el-select>

    </el-form-item>
	<el-form-item label="楼层" prop="floorCengId">
	  <el-select v-model="dataForm.floorCengId" placeholder="请选择" @change="getFloorRoomList(dataForm.floorCengId)">
	      <el-option
	        v-for="item in floorChidDataList"
	        :key="item.id"
	        :label="item.name"
	        :value="item.id">
	      </el-option>
	  </el-select>
	</el-form-item>
    <el-form-item label="房间号" prop="floorRoomid">
	  <el-select v-model="dataForm.floorRoomid" placeholder="请选择" >
	      <el-option
	        v-for="item in floorRoomDataList"
	        :key="item.id"
	        :label="item.roomSign+item.roomNum"
	        :value="item.id">
	      </el-option>
	  </el-select>
    </el-form-item>

    <el-form-item label="关联号" prop="associationNumber">
      <el-input v-model="dataForm.associationNumber" placeholder="关联号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
				floorDataList:[],
				floorRoomDataList:[],
        dataForm: {
          id: 0,
          floorId: '',
          floorRoomid: '',
          floorCengId: '',
          associationNumber: '',
          sellerId: ''
        },
		floorChidDataList:[],
        dataRule: {
          floorId: [
            { required: true, message: '楼栋不能为空', trigger: 'blur' }
          ],
          floorRoomid: [
            { required: true, message: '房间号不能为空', trigger: 'blur' }
          ],
          floorCengId: [
            { required: true, message: '楼层不能为空', trigger: 'blur' }
          ],
          associationNumber: [
            { required: true, message: '关联号不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '商家不能为空', trigger: 'blur' }
          ]
        }
      }
    },
	created() {
		this.getFloorList("0");
	},
    methods: {
		getFloorRoomList(id){

			this.$http({
			  url: this.$http.adornUrl('/hotel/thotelfloorroom/getByFloorId'),
			  method: 'get',
			  params: this.$http.adornParams({
							id:id
						})
			}).then(({ data }) => {
			  if (data && data.code === 0) {

			    this.floorRoomDataList = data.page;
			  } else {
			    this.floorRoomDataList = [];
			  }
			});
		},
		getFloorList(type) {
		  this.$http({
		    url: this.$http.adornUrl('/hotel/thotelfloor/datald'),
		    method: 'get',
		    params: this.$http.adornParams({
				type:type
			})
		  }).then(({ data }) => {
		    if (data && data.code === 0) {
		      this.floorDataList = data.data;
		    } else {
		      this.floorDataList = [];
		    }
		  });
		},
		getFloorChidList(type,id) {
		  this.$http({
		    url: this.$http.adornUrl('/hotel/thotelfloor/datald'),
		    method: 'get',
		    params: this.$http.adornParams({
				type:type,
				id:id
			})
		  }).then(({ data }) => {
		    if (data && data.code === 0) {
		      this.floorChidDataList = data.data;
		    } else {
		      this.floorChidDataList = [];
		    }
		  });
		},



      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/tdoorlock/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {

                this.dataForm.floorId = data.tDoorLock.floorId
                this.dataForm.floorRoomid = data.tDoorLock.floorRoomid
                this.dataForm.floorCengId = data.tDoorLock.floorCengId
                this.dataForm.associationNumber = data.tDoorLock.associationNumber
                this.dataForm.sellerId = data.tDoorLock.sellerId

				this.getFloorList("0")
				this.getFloorChidList("1",this.dataForm.floorId)
				this.getFloorRoomList(this.dataForm.floorCengId)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/tdoorlock/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'floorId': this.dataForm.floorId,
                'floorRoomid': this.dataForm.floorRoomid,
                'floorCengId': this.dataForm.floorCengId,
                'associationNumber': this.dataForm.associationNumber,
                'sellerId': this.dataForm.sellerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
